<?php

namespace App\Exceptions;

use App\Enum\APIEnum;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [//
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable $exception
     *
     * @return void
     *
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        //parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Throwable               $exception
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {
        /*记录日志*/
        log_arr([
            'code'           => $exception->getCode(),
            'file'           => $exception->getFile(),
            'line'           => $exception->getLine(),
            'msg'            => $exception->getMessage(),
            'request_method' => request()->method(),
            'request_url'    => request()->url(),
            'request_query'  => request()->getQueryString(),
            'request_raw'    => request()->getContent()
        ], 'error', true);
        $trace = [];
        if (config('app.debug') === true) {
            $trace = [
                'trace' => [
                    'msg'   => $exception->getMessage(),
                    'line'  => $exception->getLine(),
                    'file'  => $exception->getFile(),
                    'trace' => collect($exception->getTrace())->take(5)
                ]
            ];
        }
        /*调用返回处理*/
        throw new APIHttpException('服务异常,请稍后重试~', APIEnum::ERR['code'], [], $trace, true);
    }
}
